package top.mccsdl.Acbot.eventListener.listeners;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson2.JSONObject;

import cn.hutool.core.date.DateTime;
import cn.hutool.http.HttpRequest;
import lombok.extern.slf4j.Slf4j;
import top.mccsdl.Acbot.config.BotConfig;
import static top.mccsdl.Acbot.config.BotConfig.DOMAIN;
import static top.mccsdl.Acbot.config.BotConfig.MCCSDL;
import static top.mccsdl.Acbot.config.BotConfig.POST_CHANNELS_PUB_MSG;
import top.mccsdl.Acbot.config.EventName;
import top.mccsdl.Acbot.entity.Message;
import top.mccsdl.Acbot.eventListener.EventListener;
import top.mccsdl.Acbot.service.MenuService;
import top.mccsdl.Acbot.service.UserService;
import static top.mccsdl.Acbot.utils.DesensitizationUtil.channelMsgAndSplit;

/**
 * 频道At事件
 */
@Service
@EventName("AT_MESSAGE_CREATE")
@Slf4j
public class AtMessageCreateListener implements EventListener {

    @Autowired
    private BotConfig botConfig;

    @Autowired
    private MenuService menuService;
    @Autowired
    private UserService userService;

    @Override
    public void onEvent(JSONObject eventData) {
        String msgId = eventData.getString("id");//消息ID
        String channelId = eventData.getString("channel_id");//频道ID
        String content = eventData.getString("content");//消息内容
        JSONObject userInfo = eventData.getJSONObject("author");
        String userId = userInfo.getString("id");//pdid 用户的频道ID
        String userName = userInfo.getString("username");//用户名
        String avatar = userInfo.getString("avatar");//头像
        String union_openid = userInfo.getString("union_openid");//用户唯一ID
        String[] msg = channelMsgAndSplit(content);
        Message message = new Message();
        message.setMsgId(msgId);//被动消息
        //自己实现吧 我的删了
        // UserInfo user  = userService.channelPubGetUserInfo(userId, union_openid, channelId, message);
        log.info(DateTime.now() + "频道:[{}]({})->{}", userName, union_openid, content);//用户艾特调用日志

        //发送表情
//        AcHttpUtils.putEmoji(msgId, channelId, message);
        switch (msg[0]) {
            case "领取", "/领取" ->
                message.setContent("福利领取成功~");
            case "菜单", "/菜单" ->
                message.setContent("这是菜单~");
            case "绑定", "/绑定" ->
                message.setContent("绑定了~");
            case "查看", "/查看" ->
                message.setContent("看吧~");
            case "初始化", "/初始化" ->
                message.setContent("初始化了~");
            case "商城", "/商城" ->
                message.setImage(MCCSDL + "/show");
            default ->
                message.setContent("哎呀,未知命令捏");
        }

        String result = HttpRequest.post(String.format(DOMAIN + POST_CHANNELS_PUB_MSG, channelId))
                .body(message.toJsonStr())
                .header("Authorization", "QQBot " + botConfig.getAccessToken())
                .execute().body();
        log.info("频道At消息事件发送返回===>" + result);
    }

}
